/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package orders

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	//"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *LogisticsCompany) RecodeById(id int64) *LogisticsCompany {
	item := new(LogisticsCompany)
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil {
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *LogisticsCompany) AddRecode(item2add *LogisticsCompany) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *LogisticsCompany) AddRecodeWithsession(session *xorm.Session, item2add *LogisticsCompany) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(硬删除)*/
func (t *LogisticsCompany) DelRecodeById(id int64) bool {
	item := new(LogisticsCompany)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *LogisticsCompany) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(LogisticsCompany)
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 更新
 * -----------------*/
func (t *LogisticsCompany) UpdateRecode(rc *LogisticsCompany) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *LogisticsCompany) UpdateRecodeWithSession(session *xorm.Session, rc *LogisticsCompany) bool {
	_, err := session.ID(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/*查询表名, 注意，函数名不能定义未TableName()，因为会与xorm内部使用的TableName冲突*/
func (t *LogisticsCompany) TName() string {
	tname := lego.GetDBEngine().TableName(t)
	return tname
}

func (t *LogisticsCompany) ListForSelect() []*LogisticsCompany {

	var logistics []*LogisticsCompany = []*LogisticsCompany{}
	session := lego.GetDBEngine().Table("tb_logistics_company").Alias("t1").
		Select("t1.*").
		Where("t1.if_del = ?", 0)
	err := session.Find(&logistics)
	if err != nil {
		lego.LogError(err)
	}

	return logistics
}